﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebSysCore;

namespace HSchSchedule.FrontPage
{
    public partial class NewsManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            List<New> lst = DataBase.GetAllNews();
            gridNews.DataSource = lst;
            gridNews.DataBind();
        }

        protected void gridNews_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                New enNew = (New)e.Row.DataItem;

                Label lblID = (Label)e.Row.FindControl("lblID");
                lblID.Text = enNew.NewsID.ToString();

                Label lblTitle = (Label)e.Row.FindControl("lblTitle");
                lblTitle.Text = enNew.Title;

                Label lblDate = (Label)e.Row.FindControl("lblDate");
                lblDate.Text = enNew.Date.ToString();

                //HyperLink hplUpdate = (HyperLink)e.Row.FindControl("hplUpdate");
                //hplUpdate.NavigateUrl = "~/FrontPage/UpdateNews.aspx?NewsID=" + enNew.NewsID;

            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            // throw new NotImplementedException();
            Response.Redirect("~/FrontPage/AddNews.aspx");
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
           // throw new NotImplementedException();
            foreach (GridViewRow r in gridNews.Rows)
            {
                CheckBox chkDel = (CheckBox)r.FindControl("chkDel");
                if (chkDel.Checked)
                {
                    Label lblID = (Label)r.FindControl("lblID");
                    int id = int.Parse(lblID.Text);
                    try
                    {
                        DataBase.DeleteNews(id);
                    }
                    catch
                    {
                        lblError.Text = "Xóa không thành công.";
                    }
                }
            }
            Response.Redirect("~/FrontPage/NewsManage.aspx");
        }


    }
}
